<template>
  <div>
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      show-footer
      highlight-current-row
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="700"
      :data="tableData"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column>
        <vxe-table-column field="linkMan" title="车队名称" min-width="60" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.linkMan" placeholder="请输入车队名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="teamName" title="车队联系人" min-width="60" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.teamName" placeholder="请输入姓名" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="teamPhone" title="联系人手机号" min-width="60" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.teamPhone" placeholder="请输入手机" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="driverName" title="司机姓名" min-width="60" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.driverName" placeholder="请输入司机姓名" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="driverPhone" title="司机手机号" min-width="60" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.driverPhone" placeholder="请输入司机手机号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="licenseNumber" title="车牌号" min-width="60" sortable />
        <template v-slot:header>
          <el-input v-model="listQuery.licenseNumber" placeholder="请输入车牌号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="type" title="记录类型" :formatter="formatterType" min-width="60" sortable />
        <template v-slot:header>
          <el-select v-model="listQuery.type" placeholder="请选择" clearable>
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="status" title="状态" :formatter="formatterStatus" min-width="60" sortable />
        <template v-slot:header>
          <el-select v-model="listQuery.status" placeholder="请选择" clearable>
            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </template>
      </vxe-table-column>

      <vxe-table-column v-if="!parentData.showInfo" title="操作" fixed="right">
        <template slot-scope="scope">
          <el-tooltip v-if="scope.row.type === 2 && scope.row.status === 0" class="item" effect="dark" content="同意">
            <vxe-button type="text" @click="agree(scope.row.code)">
              同意
            </vxe-button>
          </el-tooltip>
          <el-tooltip v-if="scope.row.type === 2 && scope.row.status === 0" class="item" effect="dark" content="拒绝">
            <vxe-button type="text" @click="refuse(scope.row.code)">
              拒绝
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>

    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import XEUtils from 'xe-utils'
import VXEUtils from 'vxe-utils'
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ['cookie'] })
import request from '@/utils/request'
export default {
  name: 'ApplyRecord',
  components: {

  },
  props: {
    parentData: {
      showInfo: false
    }
  },
  data() {
    return {
      message: '',
      loading: false,
      tableData: [],
      copy: false,
      listQuery: {
        driverCode: '',
        linkMan: '',
        teamName: '',
        teamPhone: '',
        driverName: '',
        driverPhone: '',
        licenseNumber: '',
        type: '',
        status: '',
        pageNum: 1,
        pageSize: 10,
        sortField: '',
        sortType: '',
        total: 0
      },
      // 记录类型
      typeOptions: [
        { label: '申请加入', value: 1, color: 'gray' },
        { label: '邀请加入', value: 2, color: '#0000ff' }
      ],
      // 状态
      statusOptions: [
        { label: '未处理', value: 0, color: 'gray' },
        { label: '已同意', value: 1, color: '#0000ff' },
        { label: '已拒绝', value: 2, color: '#0000ff' }
      ]
    }
  },
  created() {
    this.listQuery.driverCode = this.$route.query.partyCode
    this.findList()
  },
  methods: {
    findList() {
      this.loading = true
      return new Promise(resolve => {
        request({
          url: '/arpa-basic-api/inviteApplyRecord/inviteApplyList',
          method: 'get',
          params: this.listQuery
        }).then(response => {
          this.tableData = response.data.records
          this.listQuery.total = response.data.total
          this.loading = false
          resolve();
        })
      })
    },
    // 同意
    agree(code) {
      this.$confirm('确认加入吗？').then(_ => {
        request({ url: '/arpa-basic-api/inviteApplyRecord/agreeOrRefuse', method: 'post', params: { code: code, status: 1 }}).then(response => {
          this.$message({ message: '加入成功', type: 'success' })
          this.findList()
        })
      })
    },
    // 拒绝
    refuse(code) {
      this.$confirm('确认拒绝吗？').then(_ => {
        request({ url: '/arpa-basic-api/inviteApplyRecord/agreeOrRefuse', method: 'post', params: { code: code, status: 2 }}).then(response => {
          this.$message({ message: '操作成功', type: 'success' })
          this.findList()
        })
      })
    },
    // 翻译记录类型
    formatterType({ cellValue }) {
      const item = this.typeOptions.find(
        item => item.value === cellValue
      )
      return item ? item.label : ''
    },
    // 翻译状态
    formatterStatus({ cellValue }) {
      const item = this.statusOptions.find(item => item.value === cellValue)
      return item ? item.label : ''
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1;
      this.findList();
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ''
      }
      this.findList()
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    }
  }
}
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis), .vxe-table .vxe-footer--column:not(.col--ellipsis), .vxe-table .vxe-header--column:not(.col--ellipsis) {
 padding: 6px 0;
}
.vxe-toolbar{padding:0 0 10px 0}
</style>
